package com.kaifamiao.type;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BooleanTypeHandler extends BaseTypeHandler<Boolean> {

    private static final String TRUE_VALUE = "Y";
    private static final String FALSE_VALUE = "N";

    @Override
    public void setNonNullParameter(PreparedStatement ps, int index, Boolean value, JdbcType type) throws SQLException {
        ps.setString( index, value ? TRUE_VALUE : FALSE_VALUE );
    }

    @Override
    public Boolean getNullableResult(ResultSet rs, String label) throws SQLException {
        String married = rs.getString(label);
        return TRUE_VALUE.equals( married );
    }

    @Override
    public Boolean getNullableResult(ResultSet rs, int index) throws SQLException {
        String married = rs.getString(index);
        return TRUE_VALUE.equals( married );
    }

    @Override
    public Boolean getNullableResult(CallableStatement cs, int index) throws SQLException {
        String married = cs.getString(index);
        return TRUE_VALUE.equals( married );
    }

}
